package h0906;

import java.util.HashSet;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 无重复字符的最长子串
 * @date 2024/9/6 19:24
 */
public class NoDuplicateCharString {
    public static void main(String[] args) {
        String s = "pwwkew";
        System.out.println(lengthOfLongestSubstring(s));
    }

    public static int lengthOfLongestSubstring(String s) {
        //这里之所以不能用linkedList是因为
        HashSet<Character> set = new HashSet<>();

        int n = s.length();
        int maxLen = 0;

        int start = 0;
        //还是使用滑动窗口的解法
        for (int end = 0; end < n; end++) {
            char c = s.charAt(end);
            while (set.contains(c)) {
                set.remove(s.charAt(start)); //移除第一个
                start++;
            }
            maxLen = Math.max(maxLen, end - start + 1);
            set.add(c);
        }
        return maxLen;
    }
}
